package enumdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-08-14
 * Time: 17:54
 */
public class Test {
    //测试枚举是否能反射
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c = Class.forName("enumdemo.TestEnum");
            Constructor<?> constructor = c.getDeclaredConstructor(String.class,int.class,String.class,int.class);
            constructor.setAccessible(true);

            TestEnum testEnum = (TestEnum)constructor.newInstance("草原绿",9);

            System.out.println(testEnum);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }

}
